import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:freedom/entity/user/verification_mode.dart';
import 'package:freedom/page/common/xpocker_tips.dart';
import 'package:freedom/util/date_util.dart';
import 'package:freedom/widget/dialog/verification_widget.dart';
import 'package:freedom/util/const_state.dart';

typedef _callBack = void Function(VerificationMode data);
typedef BuildWidget = Widget Function(CancelFunc cancelFunc);

class CustomerDialog {
  static const String SYSTEM_GROUP = "SYSTEM";
  static const String GAME_DIALOG_GROUP = "GAME_DIALOG_GROUP";
  // 用户信息弹窗
  static const String USER_INFO_DIALOG = "USER_INFO_DIALOG";
  // 游戏设置弹窗
  static const String GAME_SETTING_DIALOG = "GAME_SETTING_DIALOG";
  // 当前战绩弹窗
  static const String RECORD_DIALOG = "RECORD_DIALOG";
  // 换牌弹窗
  static const String SWAP_CARD_DIALOG = "SWAP_CARD_DIALOG";
  static const String MESSAGE_DIALOG = "MESSAGE_DIALOG";

  static String GAME_MENU_GROUP = "GAME_MENU_GROUP";

  static const String INPUT_GROUP = "INPUT_GROUP";

  static const String verificationCode = "verificationCode";

  static const String systemLoading = "systemLoading";

  static showVerification(context, _callBack callBack) {
    showDialog<Null>(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return VerificationWidget(callBack: callBack);
        });
  }

  static showText(String? tips, {Color? textColor, Color? bottomColor}) {
    textColor ??= const Color(0xffffffff);
    bottomColor ??= const Color(0xffff7400);
    if (tips == null) {
      return;
    }
    BotToast.showText(
      text: tips,
      duration: const Duration(seconds: 2),
      onlyOne: true,
      crossPage: false,
      wrapAnimation: (controller, cancelFunc, widget) {
        return XpockerTips(
            controller: controller,
            tips: tips,
            textColor: textColor,
            bottomColor: bottomColor);
      },
    );
  }

  static loading(String msg) {
    BotToast.showAnimationWidget(
        groupKey: systemLoading,
        key: UniqueKey(),
        enableKeyboardSafeArea: true,
        backButtonBehavior: BackButtonBehavior.ignore,
        clickClose: false,
        allowClick: false,
        ignoreContentClick: false,
        crossPage: false,
        toastBuilder: (context) {
          return Center(
            child: SizedBox(
              width: ScreenUtil().screenHeight / 2,
              height: ScreenUtil().screenHeight / 2,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  const CircularProgressIndicator(),
                  Text(
                    msg,
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 15.h,
                        fontWeight: FontWeight.bold),
                  )
                ],
              ),
            ),
          );
        },
        animationDuration: const Duration(milliseconds: 0),
        wrapToastAnimation: (controller, cancel, child) {
          return SafeArea(child: child);
        },
        onlyOne: true,
        backgroundColor: Colors.black26);
  }

  static showGamingDialog(BuildWidget buildWidget, {String? groupKey}) {
    BotToast.showAnimationWidget(
        groupKey: groupKey ?? GAME_DIALOG_GROUP,
        key: UniqueKey(),
        enableKeyboardSafeArea: false,
        backButtonBehavior: BackButtonBehavior.ignore,
        clickClose: false,
        allowClick: true,
        ignoreContentClick: false,
        crossPage: false,
        toastBuilder: (context) {
          return buildWidget(context);
        },
        animationDuration: const Duration(milliseconds: 0),
        wrapToastAnimation: (controller, cancel, child) {
          // ios full is error
          return child;
          // return SafeArea(child: child);
        },
        onlyOne: true);
  }

  static void closeAllLoading({String? groupKey}) {
    ConstState.getInstance()!.loadingDialogShow = false;
    if (groupKey != null) {
      BotToast.removeAll(groupKey);
    } else {
      BotToast.closeAllLoading();
    }
  }

  static void closeLoading() {
    BotToast.removeAll(systemLoading);
  }
}

class LoadingDialog extends Dialog {
  final String loadingText;

  const LoadingDialog({Key? key, this.loadingText = ""}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 弹窗的时候禁止返回
    return PopScope(
        onPopInvoked: (tag) {},
        child: Material(
          //创建透明层
          type: MaterialType.transparency, //透明类型
          child: Center(
            //保证控件居中效果
            child: SizedBox(
              width: 120.0,
              height: 120.0,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  const CircularProgressIndicator(
                    valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(
                      top: 20.0,
                    ),
                    child: Text(
                      loadingText,
                      style:
                          const TextStyle(fontSize: 12.0, color: Colors.white),
                      textAlign: TextAlign.center,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ));
  }
}
